package com.yf.dxs.wo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.yf.dxs.wo.consts.WoEventConst;
import com.yf.dxs.wo.dao.WoCardMsgMapper;
import com.yf.dxs.wo.dao.WoPhoneMsgMapper;
import com.yf.dxs.wo.entity.WoPhoneMsgDO;
import com.yf.dxs.wo.redis.RedisMqTemplate;
import com.yf.dxs.wo.req.msg.WoPhoneMsg;
import com.yf.dxs.wo.service.MsgHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 注册时候会先通知
 * 成为客户
 * 成为会员
 * 开卡
 */
@Service
@Slf4j
public class UserHandler implements MsgHandler {

    @Resource
    private WoCardMsgMapper woCardMapper;

    @Resource
    private WoPhoneMsgMapper woPhoneMapper;

    @Resource
    private RedisMqTemplate redisMqTemplate;

    public void handle(String msgId, String msg, String event) {
        //手机号
        if (event.equals(WoEventConst.PHONE_BIND) || event.equals(WoEventConst.PHONE_RE_BIND)) {
            WoPhoneMsg woPhoneMsg = JSON.parseObject(msg, WoPhoneMsg.class);
            log.info("处理手机号:{},{},{}", woPhoneMsg.getWid(), woPhoneMsg.getPhone(), woPhoneMsg.getOldPhone());
            WoPhoneMsgDO entity = woPhoneMsg.toEntity();
            entity.setMsgId(msgId);
            int i = woPhoneMapper.insert(entity);
            if (i == 0) {
                log.error("处理手机号失败:{},{},{}", woPhoneMsg.getWid(), woPhoneMsg.getPhone(), woPhoneMsg.getOldPhone());
            } else {
                log.info("处理手机号成功:{},{},{}", woPhoneMsg.getWid(), woPhoneMsg.getPhone(), woPhoneMsg.getOldPhone());
                redisMqTemplate.sendMsg(entity);
            }
        }

    }
}
